package cate.game.pay;

import cate.common.table.pay.PayItemRow;
import cp.external.yzx.pay.YzxClientOrder;
import cp.solution.business.pay.PayTodoEntity;
import cate.game.GameBody;
import cate.game.role.Role;
import cp.solution.define.CurrencyCode;
import cp.solution.define.PayOrderType;
import cp.solution.define.PlatformType;

import java.util.Date;

/** 支付数据装配 */
public class PayDataAssembler {

    public static PayTodoEntity buildTodo(GameBody game, Role role, PayItemRow pir, int num) {
        PayTodoEntity t = new PayTodoEntity();
        t.orderType = PayOrderType.NORMAL;
        t.platformType = PlatformType.YZX.value;
        t.platformOpenId = role.getBase().openId;
        t.itemTid = pir.id;
        t.itemNum = num;
        t.itemName = pir.name;
        t.itemNameCn = pir.nameCn;
        t.itemPrice = pir.price;
        t.createTime = new Date();
        t.srvId = game.config.srvId;
        t.srvGroupCode = game.config.gs.getGroupCode();
        t.srvName = game.config.gs.getName();
        t.gameId = role.getBase().gameId;
        t.channelCode = role.getBase().channelCode;
        t.accountId = role.getBase().accountId;
        t.roleId = role.getUid();
        t.roleName = role.getBase().name;
        t.roleLevel = role.getBase().level;
        t.roleVip = role.getBase().vip;
        t.roleCreateTime = new Date(role.getStatus().createTime);
        t.currencyCode = CurrencyCode.CNY.value;
        t.tradeCode = pir.tradeCode;
        t.langCode = role.getBase().langCode;
        t.note = "嘿";
        t.redirect = game.config.redirect;
        t.srvOpenDays = role.getGame().getOpenDay();
        t.callTimes = 0;
        return t;
    }

    public static YzxClientOrder buildClientOrder(PayTodoEntity todo, String secretKey, int version) {
        YzxClientOrder order = new YzxClientOrder();
        order.warename = todo.itemName;
        order.count = todo.itemNum;
        order.paybill = todo.itemPrice * todo.itemNum;
        order.extstr = todo.buildContext();
        order.serverid = ""+todo.srvId;
        order.servername = todo.srvName;
        order.roleid = todo.roleId;
        order.rolename = todo.roleName;
        order.rolelevel = ""+todo.roleLevel;
        order.currency = todo.currencyCode;
        order.tradecode = todo.tradeCode;
        order.cptradecode = todo.tradeCode;
        // order.props
        order.openid = todo.platformOpenId;

        order.signature = order.genSign(secretKey, version);
        return order;
    }

}
